package com.chinaums.fsp.sdk.request;

import java.io.InputStream;
import java.security.KeyStore;
import java.security.cert.Certificate;
import java.text.SimpleDateFormat;
import java.util.Date;

import com.chinaums.fsp.exception.NotFoundException;
import com.chinaums.fsp.sdk.base.RequestData;
import com.chinaums.fsp.sdk.base.ResponseDataWrapper;
import com.chinaums.fsp.sdk.base.SignType;
import com.chinaums.fsp.sdk.ca.CaGenerator;

import junit.framework.TestCase;

public class BaseRequest extends TestCase {
	protected Certificate	serverCertificate;
	protected KeyStore		clientKeyStore;
	protected final String	KEYSTORE_PASSWORD	= "123456";
	protected final String	PRIVATEKEY_PASSWORD	= "123456";
	protected final String	appId				= "8013302000000013A757BE6";
	// protected final String	url					= "http://127.0.0.1:9086/api/service.json";
	protected final String	url					= "http://218.5.69.218:8088/fspapi/api/v1/service.json";
	// 对帐文件请求地址
	protected final String	settle_url					= "http://218.5.69.218:8088/fspapi/api/settle.json";
	protected final String callbackUrl = "";
	protected final String version = "1.0.0";
	protected final String countWalletAccountNo = "01001000002";
	protected final String CMBC_WALLET_ACCOUNT_NO = "695603264000105";
	protected RequestFactory FACTORY;
	
	// 网签生产参数
//	protected Certificate	serverCertificate;
//	protected KeyStore		clientKeyStore;
//	protected final String	KEYSTORE_PASSWORD	= "135790";
//	protected final String	PRIVATEKEY_PASSWORD	= "135790";
//	protected final String	appId				= "80135020000000100000000";
//	protected final String	url					= "http://61.183.65.67:8088/fspapi/api/v1/service.json";
//	protected final String callbackUrl = "";
//	public final String version = "1.0.0";
//	protected final String countWalletAccountNo = "01001592000000001";
//	protected final String CMBC_WALLET_ACCOUNT_NO = "695603264000105";
//	protected RequestFactory FACTORY;
	
	// 测试生产参数
//	protected Certificate	serverCertificate;
//	protected KeyStore		clientKeyStore;
//	protected final String	KEYSTORE_PASSWORD	= "123456";
//	protected final String	PRIVATEKEY_PASSWORD	= "123456";
//	protected final String	appId				= "80135020000000300000000";
//	protected final String	url					= "http://61.183.65.67:8088/fspapi/api/v1/service.json";
//	protected final String callbackUrl = "";
//	public final String version = "1.0.0";
//	protected final String countWalletAccountNo = "01001000004";
//	protected final String CMBC_WALLET_ACCOUNT_NO = "695603264000105";
//	protected RequestFactory FACTORY;
	@Override
	protected void setUp() throws Exception {
		super.setUp();
		this.serverCertificate = CaGenerator
				.getCertificate(this.getInputStream("/certs/fsp_server.cer"));
		this.clientKeyStore = CaGenerator.getKeyStore(
				CaGenerator.KEYSTORE_PKCS12,
//				this.getInputStream("/certs/xmums_test.pfx"),
				this.getInputStream("/certs/fsp_client.pfx"),
				KEYSTORE_PASSWORD.toCharArray());
		
		FACTORY = new RequestFactory(appId, callbackUrl, version, url, CaGenerator.getPrivateKey(clientKeyStore, PRIVATEKEY_PASSWORD), this.serverCertificate.getPublicKey());
	}

	protected InputStream getInputStream(String path) throws Exception {
		InputStream in = BaseRequest.class.getResourceAsStream(path);
		if (null == in) {
			throw new NotFoundException("未找到" + path + "证书");
		}
		return in;
	}

	protected RequestData.Builder createRequestDataBuilder() {
		String time = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
		String nanoTime = System.nanoTime() + "";
		RequestData.Builder builder = RequestData.newBuilder();
		builder.setAppId(appId).setCallbackUrl("")
				.setClientTransId(
						time + nanoTime.substring(nanoTime.length() - 6))
				.setSignType(SignType.RSA)
				.setTransTimestamp(System.currentTimeMillis())
				.setVersion("1.0.0");
		return builder;
	}

	protected void print(ResponseDataWrapper rdw) {
		StringBuffer sb = new StringBuffer();
		sb.append(rdw.toString())
		  .append("\r\n");
		if (null != rdw.getResponseData()) {
			sb.append(rdw.getResponseData())
			  .append("\r\n");
		}
		System.out.println(sb.toString());
	}
}
